package com.example.juc;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicStampedReference;

class Book2 {
    private String title;
    private double price;

    public Book2(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book2{" +
                "title='" + title + '\'' +
                ", price=" + price +
                '}';
    }
}

// ABA 问题解决
public class AtomicStampedReferenceTest {
    public static void main(String[] args) {
        Book book = new Book("JAVA JUC", 68.8);
        Book book2 = new Book("JDBC", 18.8);
        AtomicStampedReference<Book> ref = new AtomicStampedReference<>(book, 1);
        System.err.println("[x引用内容修改]" + ref.compareAndSet(book, book2, 3, 5));
        System.err.println("[x原子引用数据]" + ref.getStamp() + "、存储内容: " + ref.getReference());

        System.err.println("[√引用内容修改]" + ref.compareAndSet(book, book2, 1, 2));
        System.err.println("[√原子引用数据]" + ref.getStamp() + "、存储内容: " + ref.getReference());
    }
}
